Ext.define('Zm.view.model.Catalog',{
    alias:'widget.modelcatalog',
    extend:'Ext.tree.Panel',
    requires:['Zm.i18n.Lang','Zm.store.Catalog','Ext.dd.DropZone','Ext.tree.plugin.TreeViewDragDrop'],
    
    store:'Catalog',
    droppedRecords: undefined,
    initComponent: function(){
        var me = this,
            cm = Zm.Common,
            lang = Zm.i18n.Lang;
        me.contextMenu = Ext.create('Ext.menu.Menu', {
            items: [{
                text:lang.tAdd,
                iconCls:'catadd',
                handler:Ext.bind(me.doAdd,me)
            },{
                text:lang.tDel,
                iconCls:'catdel',
                handler:Ext.bind(me.doDel,me)
            },{
                text:lang.tEdit,
                iconCls:'catedit',
                handler:Ext.bind(me.doEdit,me)
            },'-',{
                text:lang.tRefresh,
                iconCls:'refresh',
                handler:Ext.bind(me.doRef,me)
            }]
        });
        Ext.apply(me,{
            selModel:{
                listeners:{
                    selectionchange:{
                        fn:me.onSelChange,
                        scope:me
                    }
                }
            },
            viewConfig: {
                plugins: { 
                    ptype: 'treeviewdragdrop',
                    dropGroup: 'ddGroup',
                    enableDrag: false,
                    appendOnly:true
                },
                listeners: {
                    beforedrop: function(node, data, overModel, dropPos, opts) {
                        me.droppedRecords = data.records;
                        data.records = [];
                    },
                    drop: function(node, data, overModel, dropPos, opts) {
                        var addrids = [];
                        Ext.iterate(me.droppedRecords, function(record) {
                            addrids.push(record.get('ddid'));
                        });
                        me.fireEvent('recorddrop',me,addrids.join(','),overModel.get('cid'),data.event.ctrlKey);
                        me.droppedRecords = undefined;
                    },
                    itemcontextmenu:me.onItemContextMenu,
                    containercontextmenu:me.onContextMenu                    
                }
            },
            rootVisible: false,
            useArrows: true
        });
        me.callParent(arguments);
        me.addEvents('recorddrop','change','catadd','catdel','catedit','catrefresh');
    },
    onSelChange: function(selModel, selections){
        var me = this,
            flag = selections.length === 0;
        if(!flag){
            me.fireEvent('change',me,selections[0]);
        }
    },
    onItemContextMenu:function(view, rec, node, index, e){
        var me = view.up('modelcatalog');
        e.stopEvent();
        me.contextMenu.showAt(e.getXY());
        return false;
    },
    onContextMenu:function(view,e,eOpts){
        var me = view.up('modelcatalog');
        e.stopEvent();
        me.contextMenu.showAt(e.getXY());
        return false;
    },
    doAdd:function(){
        var me = this,view,
            selMode = me.getSelectionModel(),
            node = selMode.getLastSelected();
        if(node){
            
        }else{
            node = me.getRootNode();
        }
        me.fireEvent('catadd',me,node);
    },
    doDel:function(){
        var me = this,view,
            selMode = me.getSelectionModel(),
            node = selMode.getLastSelected();
        if(node){
            me.fireEvent('catdel',me,node);
        }
    },
    doEdit:function(){
        var me = this,view,
            selMode = me.getSelectionModel(),
            node = selMode.getLastSelected();
        if(node){
            me.fireEvent('catedit',me,node);
        }
    },
    doRef:function(){
        var me = this;
        me.fireEvent('catrefresh',me);
    }
        
});